package com.aspsine.multithreaddownload.slice;

import com.aspsine.multithreaddownload.ResourceTable;
import com.aspsine.multithreaddownload.db.DataBaseManager;
import com.aspsine.multithreaddownload.db.ThreadInfo;
import com.aspsine.multithreaddownload.demo.util.ShowToast;
import com.aspsine.multithreaddownload.util.L;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.ability.FormException;
import ohos.aafwk.content.Intent;
import ohos.agp.components.Button;
import ohos.agp.components.Component;
import ohos.agp.components.Text;
import java.util.List;


public class DemoAbilitySlice extends AbilitySlice {

    private static final String TAG = MainAbilitySlice.class.getName();
    private Button add;

    private Button delete;

    private Button query;

    private Button update;

    private Text text;

    private Text title;

    private DataBaseManager baseManager;


    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_main_demo);
        initButton();
        initDataDao();
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }

    private void initButton() {
        text = (Text)findComponentById(ResourceTable.Id_text);
        add = (Button) findComponentById(ResourceTable.Id_add);
        add.setClickedListener(this::add);
        delete = (Button)findComponentById(ResourceTable.Id_delete);
        delete.setClickedListener(this::delete);
        query = (Button)findComponentById(ResourceTable.Id_query);
        query.setClickedListener(this::query);
        update = (Button)findComponentById(ResourceTable.Id_update);
        update.setClickedListener(this::update);
    }

    public void download(Component component){

    }

    public void initDataDao(){
        baseManager = DataBaseManager.getInstance(this);
    }

    public void delete(Component component){
        adjust(component);
        baseManager.delete("111");
    }

    public void update(Component component){
        adjust(component);
        L.d("11111update","update");
        baseManager.update("111",1,1000);
        //getListThreadInfo(component);
    }
    public void add(Component component){
        adjust(component);
        L.d(TAG,"add");
        ThreadInfo threadInfo = new ThreadInfo();
        threadInfo.setId(1);
        threadInfo.setTag("111");
        threadInfo.setUri("wwww.baidu.com");
        threadInfo.setStart(1);
        threadInfo.setEnd(100);
        threadInfo.setFinished(20);
        baseManager.insert(threadInfo);
    }
    static int flga = 0;
    public void adjust(Component component){
        if(flga == 0){
            flga++;
            component.setAlpha(0.3f);
        }else {
            component.setAlpha(1.0f);
            flga=0;
        }

    }
    void query(Component component){
        getListThreadInfo(component);
    }

    public void getListThreadInfo(Component component){
        adjust(component);
        L.d(TAG,"queryThredaInfo");
        List<ThreadInfo> list = baseManager. getThreadInfos();
        Boolean flag = baseManager.exists("111",1);
        ShowToast.showToast(flag+"");
        if(null == list){
            return;
        }
        StringBuilder appendStr = new StringBuilder();

        for(ThreadInfo info:list){
            L.d(TAG,"id= "+info.getId());
            L.d(TAG,"tag= "+info.getTag());
            L.d(TAG,"Uri= "+info.getUri());
            L.d(TAG,"start= "+info.getStart());
            L.d(TAG,"end= "+info.getEnd());
            L.d(TAG,"finished= "+info.getFinished());
            appendStr.append(
                    "size="+list.size()+
                    "finished="+info.getFinished()+System.lineSeparator()
                            +"id="+info.getId()+System.lineSeparator()
                            +"tag="+info.getTag()+System.lineSeparator()
                            +"Uri="+info.getUri()+System.lineSeparator()
                            +"start="+info.getStart()+System.lineSeparator()
                            +"end="+info.getEnd()+System.lineSeparator());
        }
        text.append("");
        text.setText(appendStr.toString());
    }



}
